---
--- Created by mick.
--- DateTime: 17-11-22 下午2:03
---

local _OBJ = {}

function _OBJ.get(self)
    local cookies = ngx.header["Set-Cookie"] or {}
    if type(cookies) == "string" then
        cookies = {cookies}
    end
    return cookies
end

--添加cookie值
function _OBJ.add(self,name,value,expire,path)
    expire = expire or 86400
    path = path or "/"
    cookie = ""..name.."="..value.."; Path="..path.."; Expires=" ..ngx.cookie_time(ngx.time() + expire)
    local cookies = self:get()
    table.insert(cookies, cookie)
    ngx.header["Set-Cookie"] = cookies
end

--获取cookie，返回table
function _OBJ.get_table(self)
    local cookie_s = header["cookie"] or nil
    local cookie_t = {}
    if not cookie_s then
        return {}
    else
        local tt = lfc.split(_, cookie_s, ";")
        for i, v in ipairs(tt) do
            local cookie_t_item = lfc.split(_, v, "=")
            cookie_t[lfc.trim(_,cookie_t_item[1])] = lfc.trim(_,cookie_t_item[2])
        end
    end
    return cookie_t

end

--更新cookie的过期时间
function _OBJ.update(self)
    for i, v in pairs(self:get_table()) do
        self:add(i, v)
    end
end

return _OBJ